package com.mysoft.gfyx.clue.controller;

import com.mysoft.framework.modeling.dataapi.DataApi;
import com.mysoft.framework.modeling.dto.OptionItem;
import com.mysoft.framework.mvc.api.Controller;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.clue.controller.dto.Perm.GetUserAreaPermissionResponseDto;
import com.mysoft.gfyx.clue.controller.dto.Perm.SaveProfitProvinceRequestDto;
import com.mysoft.gfyx.clue.service.AreaOptionService;
import com.mysoft.gfyx.clue.service.DataPermissionService;
import com.mysoft.gfyx.clue.service.ProfitProvinceService;
import com.mysoft.gfyx.clue.service.dto.GetProfitProvinceDto;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;


@PubService(value = "/profitProvinceDataApi", prefix = RequestPrefix.API)
@Tag(name = "权益省份-API")
public class ProfitProvinceDataApiController extends Controller implements DataApi {

    @Autowired
    private ProfitProvinceService profitProvinceService;
    @Autowired
    private DataPermissionService dataPermissionService;
    @Autowired
    private AreaOptionService areaOptionService;

    @PubAction(value = "/getProfitProvince", method = RequestMethod.POST)
    public GetProfitProvinceDto getProfitProvinceNum() {
        return profitProvinceService.getProfitProvince();
    }

    /**
     * 获取省份权益，全国权益的返回全国所有省份，部分省份权益的，返回部分省份
     *
     * @return
     */
    @PubAction(value = "/getProfitProvinceList", method = RequestMethod.POST)
    public List<OptionItem> getProfitProvinceList() {
        // 租户权益包含的省份
        List<OptionItem> profitProvinceList = profitProvinceService.getProfitProvinceList();
        // 用户拥有的权限地区（只包含省）
        GetUserAreaPermissionResponseDto userAreaPermissionDto = dataPermissionService.getUserPermissionArea();

        List<OptionItem> result = new ArrayList<>();
        if (userAreaPermissionDto == null
                || userAreaPermissionDto.getArea() == null
                || userAreaPermissionDto.getArea().contains("0")
                || userAreaPermissionDto.getArea().size() == 0) {
            return profitProvinceList;
        }

        //
        profitProvinceList.forEach(p -> {
            if (userAreaPermissionDto.getArea().contains(p.getValue())) {
                //取权益和权限的交集
                result.add(p);
            }
        });

        return result;
    }

    @PubAction(value = "/saveProfitProvince", method = RequestMethod.POST)
    public void saveProfitProvince(@RequestBody SaveProfitProvinceRequestDto dto) {
        profitProvinceService.saveProfitProvince(dto);
    }

    @PubAction(value = "/provinceOption", method = RequestMethod.POST)
    public List<OptionItem> provinceOption() {
        return profitProvinceService.provinceOption();
    }

    @PubAction(value = "/getProfitProvinceAndCity", method = RequestMethod.POST)
    public List<OptionItem> getProfitProvinceAndCity() {
        return areaOptionService.getProfitWithPermissionAreaData();
    }
}
